import { GSprite } from "../../../../com/mui/controls/GSprite";
import { ItemConfig } from "../../resource/ItemConfig";
import { ECategory } from "../../../../Message/Public/ECategory";
import { EProp } from "../../../../Message/Public/EProp";
import { ItemData } from "../../resource/info/ItemData";
import { ObjCreate } from "../common/ObjCreate";
import { ImagesConst } from "../../resource/ImagesConst";
import { FilterConst } from "../../../component/gconst/FilterConst";
import { MsgManager } from "../../manager/MsgManager";
import { Language } from "../../../../extend/language/Language";
import { GameDefConfig } from "../../resource/GameDefConfig";
import { Alert } from "../../../../com/mui/controls/Alert";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { EAmuletSpiritOper } from "../../../../Message/Game/EAmuletSpiritOper";
import { FahunItemInfo } from "./FahunItemInfo";
import { AmuletSpiritConfig } from "../../resource/AmuletSpiritConfig";
import { Cache } from "../../cache/Cache";
import { ItemsUtil } from "../shortcuts/item/ItemsUtil";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { HTMLUtil } from "../../../../com/gengine/utils/HTMLUtil";
import { FahunItem } from "./FahunItem";
import { DeployPanelItem } from "../equipment/deploy/DeployPanelItem";
import { GlobalSkin } from "../../../component/skin/GlobalSkin";
import { LoaderHelp } from "../../../common/display/LoaderHelp";
import { ResFileConst } from "../../resource/ResFileConst";
import { GTextFiled } from "../../../../com/mui/controls/GTextFiled";
import { GButton } from "../../../../com/mui/controls/GButton";
import { TAmuletSpiritConfig } from "../../../../Message/Db/Tables/TAmuletSpiritConfig";
import { _EAmuletSpiritBreak } from "../../../../EAmuletSpiritOper/_EAmuletSpiritBreak";
type int = number;
//class FahunBreakPanel
    
    export  class FahunBreakPanel extends GSprite
    {
        constructor()
        {
            super();
            this._materialItemArray = [];
            var loc1=ItemConfig.instance.getItemByType(ECategory._ECategoryProp, EProp._EPropAmuletSpiritBreakStone);
            if (loc1) 
            {
                this._materialItemData = new ItemData(loc1.item.code);
            }
            return;
        }

        /* internal  */onLoadHandler(): void
        {
            ObjCreate.createBitmap(ImagesConst.FahunBLabel, 15, 135, this);
            return;
        }

        protected /* override */ disposeImpl(arg1: Boolean=true): void
        {
            return;
        }

        /* internal  */onItemClickHandler(arg1: Object /* flash.events.Event */): void
        {
            this._cItem.fahunItemInfo = null;
            this.updateMaterial(true);
            return;
        }

        /* internal  */updateMaterial(arg1: Boolean): void
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < this._materialItemArray.length) 
            {
                loc1 = this._materialItemArray[loc2];
                if (arg1) 
                {
                    loc1.itemData = null;
                    loc1.filters = null;
                }
                else if (loc2 < this._fillNum) 
                {
                    loc1.itemData = this._materialItemData;
                    loc1.amount = this._perNum;
                    if (loc2 < this._enableNum) 
                    {
                        loc1.filters = null;
                    }
                    else 
                    {
                        loc1.filters = [FilterConst.colorFilter];
                    }
                }
                else 
                {
                    loc1.itemData = null;
                    loc1.filters = null;
                }
                ++loc2;
            }
            return;
        }

        /* internal  */onBreakBtnClickHandler(arg1: Object /* flash.events.Event */): void
        {
            var e: Object /* flash.events.Event */;
            var mrArray: Array<any>;
            var tmp: int;
            var itemData: ItemData;
            var i: int;
            var onSelect2: Function;

            var loc1;
            mrArray = null;
            tmp = 0;
            itemData = null;
            i = 0;
            e = arg1;
            if (this._fahunItemInfo) 
            {
                if (!this._fahunItemInfo.isEnable) 
                {
                    MsgManager.showRollTipsMsg(Language.getStringByParam(58029, GameDefConfig.instance.getFahunMW(this._fahunItemInfo.index)));
                    return;
                }
                if (this._fahunItemInfo.isMaxLevel) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(58028));
                    return;
                }
                if (!this._fahunItemInfo.isNeedBreak) 
                {
                    onSelect2 = (arg1: int): void
                    {
                        if (arg1 == Alert.OK) 
                        {
                            Dispatcher.dispatchEvent(new DataEvent(EventName.FahunShowTab, {"operIndex": 0, "itemIndex":(_fahunItemInfo.index - 1)}));
                        }
                        return;
                    }
                    Alert.show(Language.getString(58018), null, Alert.OK | Alert.CANCEL, null, onSelect2);
                    return;
                }
                if (!this._isMoneyEnough) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(60121));
                    return;
                }
                if (!this._isMaterialEnough) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(58021));
                    return;
                }
                mrArray = [];
                i = 0;
                while (i < this._itemArray.length) 
                {
                    itemData = this._itemArray[i];
                    mrArray.push(itemData.uid);
                    tmp = tmp + itemData.itemAmount;
                    if (tmp >= this._needCount) 
                    {
                        break;
                    }
                    ++i;
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.FahunOper, {"index": this._cItem.fahunItemInfo.index, "oper": _EAmuletSpiritBreak, "materials": mrArray}));
            }
            return;
        }

        public clear(): void
        {
            this._cItem.fahunItemInfo = null;
            this.updateMaterial(true);
            this._nameTxt.text = "";
            this._addAttrTxt1.text = "";
            this._costTxt.text = "";
            return;
        }

        public updateByFahunItemInfo(arg1: FahunItemInfo): void
        {
            this._fahunItemInfo = arg1;
            this._cItem.fahunItemInfo = this._fahunItemInfo;
            if (arg1) 
            {
                this._nameTxt.text = GameDefConfig.instance.getFahunName(arg1.index);
                this._configInfo = AmuletSpiritConfig.instance.getConfigByLevel(arg1.level);
                if (this._configInfo) 
                {
                    this._needMoney = this._configInfo.breakMoney;
                    this._needCount = this._configInfo.breakMaterials;
                    this._perNum = this._needCount / 5;
                    this._addAttrTxt1.htmlText = Cache.instance.fahun.getAttrAddString(this._configInfo.attrAdd, " :");
                }
                this._fillNum = this._needCount / this._perNum;
                this.updateMaterial(false);
                if (arg1.isMaxLevel) 
                {
                    this._maxTipTxt.visible = true;
                    this._addAttrTxt1.text = "";
                }
                else 
                {
                    this._maxTipTxt.visible = false;
                }
                this.materialUpdate();
                this.moneyUpdate();
            }
            else 
            {
                this.clear();
            }
            this.moneyUpdate();
            return;
        }

        public materialUpdate(): void
        {
            this._itemCount = Cache.instance.pack.backPackCache.getItemCountByCategoryAndType(ECategory._ECategoryProp, EProp._EPropAmuletSpiritBreakStone);
            this._itemArray = Cache.instance.pack.backPackCache.getItemsByJudge(ItemsUtil.isFahunBreakItem);
            this._isMaterialEnough = this._itemCount >= this._needCount;
            this._enableNum = int(this._itemCount / this._perNum);
            if (this._cItem.fahunItemInfo) 
            {
                this.updateMaterial(false);
            }
            return;
        }

        public moneyUpdate(): void
        {
            var loc1=null;
            this._isMoneyEnough = Cache.instance.role.moneyCoin >= this._needMoney;
            if (this._isMoneyEnough) 
            {
                loc1 = GlobalStyle.color1;
            }
            else 
            {
                loc1 = GlobalStyle.color7;
            }
            this._costTxt.htmlText = HTMLUtil.addColor(this._needMoney.toString(), loc1);
            return;
        }

        protected /* override */ createDisposedChildrenImpl(): void
        {
            var loc1=null;
            super.createDisposedChildrenImpl();
            ObjCreate.createGImageBitmap(ImagesConst.LongBg, 20, 40, this);
            ObjCreate.createBg(55, 10, 175, 25, this, ImagesConst.PetTileBg);
            this._nameTxt = ObjCreate.createGTextField("", 55, 13, 180, 25, this, GlobalStyle.textFormat6);
            this._nameTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._cItem = new FahunItem();
            this._cItem.move(123, 45);
            addChild(this._cItem);
            this._cItem.addEventListener(flash.events.MouseEvent.CLICK, this.onItemClickHandler);
            var loc2=0;
            while (loc2 < 5) 
            {
                loc1 = new DeployPanelItem(loc2, true);
                loc1.label = Language.getString(58016);
                loc1.x = 33 + loc2 * 48;
                loc1.y = 103;
                this.addChild(loc1);
                this._materialItemArray.push(loc1);
                ++loc2;
            }
            ObjCreate.createBg(35, 155, 235, 105, this);
            this._addAttrTxt1 = ObjCreate.createGTextField("", 35, 172, 235, 20, this, GlobalStyle.textFormat6);
            this._addAttrTxt1.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._addAttrTxt2 = ObjCreate.createGTextField("", 172, 172, 220, 20, this, GlobalStyle.textFormat6);
            this._addAttrTxt2.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._maxTipTxt = ObjCreate.createGTextField(Language.getString(58028), 35, 200, 235, 20, this, GlobalStyle.textFormat6);
            this._maxTipTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._maxTipTxt.visible = false;
            this._breakBtn = ObjCreate.createGButton(Language.getString(58012), 103, 310, 92, 28, this, GlobalSkin.ButtonRed);
            this._breakBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBreakBtnClickHandler);
            this._costSprite = new Object /* flash.display.Sprite */();
            this._costSprite.x = 175;
            this._costSprite.y = 365;
            this.addChild(this._costSprite);
            ObjCreate.createLabel(Language.getString(60322), 0, 0, 100, 20, flash.text.TextFormatAlign.LEFT, this._costSprite, true, 15859633);
            ObjCreate.createBg(34, 1, 75, 18, this._costSprite, ImagesConst.DisabledBg);
            ObjCreate.createBitmap(ImagesConst.Tongqian_small, 87, 3, this._costSprite);
            this._costTxt = ObjCreate.createGTextField("0", 34, 1, 50, 20, this._costSprite);
            LoaderHelp.addResCallBack(ResFileConst.fahun, this.onLoadHandler);
            return;
        }

        /* internal */ /* var */_nameTxt: GTextFiled;

        /* internal */ /* var */_cItem: FahunItem;

        /* internal */ /* var */_addAttrTxt1: GTextFiled;

        /* internal */ /* var */_addAttrTxt2: GTextFiled;

        /* internal */ /* var */_breakBtn: GButton;

        /* internal */ /* var */_costSprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_costTxt: GTextFiled;

        /* internal */ /* var */_maxTipTxt: GTextFiled;

        /* internal */ /* var */_materialItemArray: Array<any>;

        /* internal */ /* var */_materialItemData: ItemData;

        /* internal */ /* var */_itemCount: int;

        /* internal */ /* var */_itemArray: Array<any>;

        /* internal */ /* var */_needCount: int;

        /* internal */ /* var */_needMoney: int;

        /* internal */ /* var */_maxLevel: int=100;

        /* internal */ /* var */_isMoneyEnough: Boolean;

        /* internal */ /* var */_isMaterialEnough: Boolean;

        /* internal */ /* var */_perNum: int=2;

        /* internal */ /* var */_configInfo: TAmuletSpiritConfig;

        /* internal */ /* var */_fillNum: int;

        /* internal */ /* var */_enableNum: int;

        /* internal */ /* var */_fahunItemInfo: FahunItemInfo;
    }
